<html>
<body>
This script will extract the Taxonomies off of the 211 website and save them to a temporary file.  

After running this script you must run the <A href="load_extracts.php">load_extracts</A> script to update the database.
<?php 

$base_URL = "http://211online.unitedwayatlanta.org/";
$agency_URL = $base_URL."KeywordList.aspx?;;0;;N;0;505022;;";
$taxonomy_link_regex_start = preg_quote ("href=\"MatchList.aspx*>");
$taxonomy_link_regex_end = preg_quote ("<");
$no_agency_regex = preg_quote ("Sorry, we were unable to find an agency or program in our database that offers");
$agency_name_regex_start = preg_quote("<div class=\"ml_data\"><div class=\"ml_name\">");
$agency_name_regex_end = preg_quote ("</div></div>");
$attr_html_start = "MatchList.aspx?k;;0;;N;0;505022;";
$max_runtime = 600;
$anchor_tag = "a";
$href_attr = "href";
$agency_tag = "div";
$agency_delim = "ml_name";
$agency_loc_delim = "ml_location";
$taxonomy_file = "extracted_taxonomies.csv";

echo "Maximum script runtime set to ".$max_runtime." seconds.<BR>";
set_time_limit($max_runtime);

echo "Upating QA.Agencies from: ".htmlentities($agency_URL)."<BR>";

$handle = @fopen($agency_URL, "r");
if ($handle) {

        $source = null;
	while (!feof($handle)){
		$buffer = fgets($handle);
		$source = $source . $buffer;
	}
	echo "Warning messages from DOMDocument::loadHTML() may safely be ignored.<BR>";
        $doc = new DOMDocument();  
        error_reporting(0);
        $doc->loadHTML($source);
        error_reporting(E_ERROR|E_WARNING|E_PARSE);
        //echo htmlentities($doc->saveHTML());
        $i = 0;
        $nodelist = new DOMNodelist();
        $nodelist = $doc->getElementsByTagName("*");
        $output = "<BR><TABLE border=1><TR><TD>Taxonomy</TD><TD>Link to Agencies</TD></TR>";
        $taxonomy_array = array();
        echo "Number of nodes in the source document ".htmlentities($nodelist->length),".<BR>";
        foreach ($nodelist as $node){
              if ( $node->nodeName == $anchor_tag  ){
                   if ($node->hasAttributes()) {
                        $node_attributes = $node->attributes;
                        $node_value = $node->nodeValue;
                        $attr_href_value = $node_attributes->getNamedItem($href_attr)->value;
                        if ( strncmp($attr_html_start,$attr_href_value,strlen($attr_html_start)) == 0) {
                             $output .= "<TR><TD>".substr($attr_href_value,strlen($attr_html_start),strlen($attr_href_value))."</TD>";
                             $output .= "<TD>".$attr_href_value."</TD></TR>";
                             $i++;
                             $taxonomy_array[substr($attr_href_value,strlen($attr_html_start),strlen($attr_href_value))] = $attr_href_value;
                        }
                   }
              }
        }
        $output .= "<TR><TD colspan=2 width=100% align=center>Records extracted ".$i."</TD></TR></TABLE>";
        echo $output."<BR><HR><BR>";
     fclose($handle);
}
$fp = fopen($taxonomy_file, 'w');

foreach ($taxonomy_array as $key => $value){
     fwrite($fp,$key.",".$value."\r\n");
}
fclose($fp);
echo "saved to ".$taxonomy_file."<BR>";


?><BR>

</body>
</html>